//
//  WriteViewController.m
//  ShangChengMoBan
//
//  Created by 军 on 16/8/26.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "WriteViewController.h"
#import "EditAddressViewController.h"
#import "ManageViewController.h"
#import "DizhiChoseView.h"

@interface WriteViewController ()<UITextViewDelegate,DiZhiChoseDelegate>
@property(nonatomic,strong)NSMutableArray *arr;
@end

@implementation WriteViewController
{
    NSMutableDictionary * dic;
    UILabel * _placeLable;
    UITextField * _call,* _name;
    UIButton * _website,* moRenButton;
    UITextView* family;
    NSArray *_diZhiAry;
    NSString *_shiFouMoRen;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _shiFouMoRen = @"0";
    [self drawNavWithTitle:@"添加新地址" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    self.view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.93 alpha:1.00];
    //保存
    UIButton * guiButton = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-60, 20, 60, 44)];
    [guiButton setTitle:@"保存" forState:UIControlStateNormal];
    [guiButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    guiButton.titleLabel.font=[UIFont systemFontOfSize:15];
    [guiButton addTarget:self action:@selector(baoButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:guiButton];
    [self take];
    
}
//设置界面
-(void)take{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 66, KScreenWidth, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    UILabel * person =[[UILabel alloc]initWithFrame:CGRectMake(10, 10,80, 30)];
    person.font = [UIFont systemFontOfSize:14];
    person.text=@"收货人";
    _name = [[UITextField alloc]initWithFrame:CGRectMake(person.right+10, 10, 100, 30)];
    _name.placeholder = @"你的姓名";
    _name.font = [UIFont systemFontOfSize:14];
    [view addSubview:person];
    [view addSubview:_name];
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(0, view.bottom+2, KScreenWidth, 50)];
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view2];
    UILabel * telephone =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
    telephone .font = [UIFont systemFontOfSize:14];
    telephone.text=@"联系电话";
    _call = [[UITextField alloc]initWithFrame:CGRectMake(telephone.right+10, 10, 100, 30)];
    _call.placeholder = @"你的手机号";
    _call.font = [UIFont systemFontOfSize:14];
    UIButton * callImage = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-30, 15, 20,20)];
    [callImage setImage:[UIImage imageNamed:@"+"] forState:UIControlStateNormal];
    [callImage addTarget:self action:@selector(call) forControlEvents:UIControlEventTouchUpInside];
    [view2 addSubview:telephone];
    [view2 addSubview:_call];
    [view2 addSubview:callImage];
    
    UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(0, view2.bottom+2, KScreenWidth, 50)];
    view3.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view3];
    UILabel * address =[[UILabel alloc]initWithFrame:CGRectMake(10, 10, 80, 30)];
    address.font = [UIFont systemFontOfSize:14];
    address.text=@"所在地区";
    _website = [[UIButton alloc]initWithFrame:CGRectMake(address.right+10, 10, KScreenWidth-address.right-40, 30)];
    [_website setTitle:@"请选择" forState:UIControlStateNormal];
    _website.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    if ([_website.titleLabel.text isEqualToString:@"请选择"]) {
        [_website setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    }else{
        [_website setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    _website.titleLabel.font = [UIFont systemFontOfSize:14];
    [_website addTarget:self action:@selector(place) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(_website.right+10, 18, 14, 14)];
    image.image = [UIImage imageNamed:@"you"];
    [view3 addSubview:_website];
    [view3 addSubview:address];
    [view3 addSubview:image];
    
    family = [[UITextView alloc]initWithFrame:CGRectMake(0, view3.bottom+2, KScreenWidth, 90)];
    family.delegate = self;
    [self.view addSubview:family];
    _placeLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
    _placeLable.text = @"请填写详细地址,不少于5个字";
    _placeLable.textColor = [UIColor lightGrayColor];
    _placeLable.font = [UIFont systemFontOfSize:13];
    _placeLable.backgroundColor = [UIColor clearColor];
    [family addSubview:_placeLable];
    UIView * lastView = [[UIView alloc]initWithFrame:CGRectMake(0, family.bottom+10, KScreenWidth,50)];
    lastView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:lastView];
    
    UILabel * moRen = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 30)];
    moRen.text = @"设为默认";
    
    moRenButton = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth-60, 10, 50, 30)];
    [moRenButton setSelected:NO];
    [moRenButton setImage:[UIImage imageNamed:@"δѡ"] forState:UIControlStateNormal];
    [moRenButton addTarget:self action:@selector(moRen:) forControlEvents:UIControlEventTouchUpInside];
    [lastView addSubview:moRenButton];
    [lastView addSubview:moRen];
    
}
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeLable.text = @"请填写详细地址,不少于5个字";
    }else{
        _placeLable.text = @"";
    }
}
//保存按钮
-(void)baoButton{
    //     dic=[[NSMutableDictionary alloc]initWithDictionary: @{@"name":name.text,@"call":call.text,@"website":website.titleLabel.text,@"family":family.text}] ;
    //
    //    if (moRenButton.selected) {
    //        [dic setObject:@"1" forKey:@"moRenButton"];
    //    }else{
    //         [dic setObject:@"0" forKey:@"moRenButton"];
    //    }
    //
    //    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    //   NSMutableArray * arr = [user mutableArrayValueForKey:@"newAdd"];
    //     [arr  addObject:dic];
    //    ManageViewController * jifen = [[ManageViewController alloc]init];
    //    [self.navigationController pushViewController:jifen animated:YES];
    
    
    if (_name.text.length == 0) {
        [[iToast makeText:@"请输入收货人姓名"]show];
        return;
        
    }
    if (_call.text.length == 0) {
        [[iToast makeText:@"请输入收货人电话"]show];
        return;
        
    }
    if (_website.titleLabel.text.length < 4) {
        [[iToast makeText:@"请选择收货地址"]show];
        return;
    }
    
    if (family.text.length < 5) {
        [[iToast makeText:@"请输入详细收货地址"]show];
        return;
    }
    
    
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    
    
    
    [requestDic setObject:[[_diZhiAry firstObject]objectForKey:@"regionId"] forKey:@"province"];
    [requestDic setObject:[[_diZhiAry objectAtIndex:1]objectForKey:@"regionId"] forKey:@"city"];
    [requestDic setObject:[[_diZhiAry objectAtIndex:2]objectForKey:@"regionId"] forKey:@"district"];
    [requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    
    [requestDic setObject:_call.text forKey:@"mobile"];
    [requestDic setObject:family.text forKey:@"address"];
    [requestDic setObject:_name.text forKey:@"consignee"];
    [requestDic setObject:_shiFouMoRen forKey:@"defaultAddress"];
    
    [TCUtils showLoadingWithView:self.view];
    [NetWorking NEWjieKouQingQiu:DZ_xinZengDiZhi ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
            
            if ([[dic objectForKey:Code]integerValue] == 200) {
                //添加地址成功
                
                [[NSNotificationCenter defaultCenter]postNotificationName:@"diZhitableShuaXin" object:nil];
                [self.navigationController popViewControllerAnimated:YES];
                
            }
        }
    }];
    
    //                NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    //
    //                NSArray *ary = [((UITextField *)[_textFieldAry objectAtIndex:0]).text componentsSeparatedByString:@"/"];
    //
    //                [requestDic setObject:[_shujuDic objectForKey:@"_id"] forKey:@"_id"];
    //                [requestDic setObject:[ary firstObject] forKey:@"provinceName"];
    //                [requestDic setObject:[ary objectAtIndex:1] forKey:@"cityName"];
    //                [requestDic setObject:[ary objectAtIndex:2] forKey:@"areaName"];
    //                [requestDic setObject:[self defaultLoadUserName] forKey:@"username"];
    //                [requestDic setObject:((UITextField *)[_textFieldAry objectAtIndex:3]).text forKey:@"shop_phone"];
    //                [requestDic setObject:((UITextField *)[_textFieldAry objectAtIndex:1]).text forKey:@"addr_detail"];
    //                [requestDic setObject:((UITextField *)[_textFieldAry objectAtIndex:4]).text forKey:@"zip_code"];
    //                [requestDic setObject:((UITextField *)[_textFieldAry objectAtIndex:2]).text forKey:@"shop_name"];
    //
    //                [NetWorking jieKouQingQiu:ChangeZhouHuoDiZhi And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
    //                    if(!error){
    //
    //                        if ([[dic objectForKey:Code]integerValue] == 200) {
    //                            //修改地址成功
    //                            [self.navigationController popViewControllerAnimated:YES];
    //                            [[NSNotificationCenter defaultCenter]postNotificationName:@"shuaXinView" object:nil];
    //                            [[NSNotificationCenter defaultCenter]postNotificationName:@"shuaXinXiuGaiView" object:nil];
    //                        }
    //                    }
    //                }];
    //            }
    //
    
}
//设置为默认
-(void)moRen:(UIButton *)button{
    button.selected = !button.selected;
    if (button.selected) {
        _shiFouMoRen = @"1";
        [button setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateNormal];
    }else{
        _shiFouMoRen = @"0";
        [button setImage:[UIImage imageNamed:@"δѡ"] forState:UIControlStateNormal];
    }
    
}
//进入通讯录
-(void)call{
    EditAddressViewController * jifen = [[EditAddressViewController alloc]init];
    [self.navigationController pushViewController:jifen animated:YES];
}
//选择地址
-(void)place{
    DizhiChoseView * diZhiView = [[DizhiChoseView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
    diZhiView.delegate = self;
    [self.view addSubview:diZhiView];
    
}
-(void)choseEndClicked:(NSArray*)ary{
    _diZhiAry = ary;
    NSString *diZhiStr = [NSString stringWithFormat:@"%@/%@/%@",[[ary objectAtIndex:0]objectForKey:@"regionName"],[[ary objectAtIndex:1]objectForKey:@"regionName"],[[ary objectAtIndex:2]objectForKey:@"regionName"]];
    [_website setTitle:diZhiStr forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
